package daw1.programacion.ud11;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Ej04 {
	private static Map<String,String> colores;
	private static void procesarEntrada() {
		FileReader fr=null;
		BufferedReader br = null;
		FileWriter fw=null;
		PrintWriter pw=null;
		try {
			fr=new FileReader("D:/workspace/asgaard/src/daw1/programacion/ud11/original.txt");
			br=new BufferedReader(fr);
			
			fw=new FileWriter("D:/workspace/asgaard/src/daw1/programacion/ud11/coloreado.htm");
			pw=new PrintWriter(fw);
			
			pw.println("<HEAD>");
			pw.println("	<link rel=\"stylesheet\" type=\"text/css\" href=\"estilos.css\">");
			pw.println("</HEAD>");
			pw.println("<BODY>");
			String linea;
			while ((linea=br.readLine()) != null) {	
				String lineaAEscribir="";
				
				String[] palabra=linea.split(" ");
				for (int i = 0; i < palabra.length; i++) {
					if (colores.containsKey(palabra[i])) {
						lineaAEscribir=lineaAEscribir+
								"<span class=\""+
								colores.get(palabra[i])+
								"\">"+
								palabra[i]+
								"</span> ";
					}
					else {
						lineaAEscribir=lineaAEscribir+palabra[i]+" ";
					}
				}
				lineaAEscribir+="<br>";
				
				pw.println(lineaAEscribir);
			}
			pw.println("</BODY>");
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			try {
				br.close();
				pw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		inicializar();
		cargarColores();
		procesarEntrada();
		generarHojaDeEstilos();
		//System.out.println(colores); //DEBUG
	}
	private static void generarHojaDeEstilos() {
		FileWriter fw=null;
		PrintWriter pw=null;
		
		try {
			fw=new FileWriter("D:/workspace/asgaard/src/daw1/programacion/ud11/estilos.css");
			pw=new PrintWriter(fw);
			Collection<String> listaColores=colores.values();
			Set<String> listaColoresNoRepetidos=new HashSet<String>();
			for (String color : listaColores) {
				listaColoresNoRepetidos.add(color);
			}
			
			for (String color : listaColoresNoRepetidos) {
				pw.println("."+ color +"{color:"+ color +";}");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			pw.close();
		}
	}
	private static void cargarColores() {

		FileReader fr=null;
		BufferedReader br=null;
		try {
			fr=new FileReader("D:/workspace/asgaard/src/daw1/programacion/ud11/palabras.txt");
			br=new BufferedReader(fr);
			
			String linea;
			while ((linea=br.readLine())!=null) {
				String[] lineaPartida=linea.split(":");
				colores.put(lineaPartida[0], lineaPartida[1]);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			try {
				br.close();
			} catch (IOException e) {

				e.printStackTrace();
			}
		}
	}
	private static void inicializar() {
		colores=new HashMap<String,String>();
		
	}
}
